1920C - Partitioning the Array - CodeForces Solution


brute force math number theory

Please click on ads to support us..

Python Code:

from math import *
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    div = []
    d = 1
    while d * d <= n:
        if n % d == 0:
            if d * d == n: div.append(d)
            else: div.append(d); div.append(n // d)
        d += 1
    cnt = 0
    for i in div:
        if i == n: cnt += 1; continue
        b = n // i
        flag = 0
        f = abs(a[0] - a[i])
        if f == 1: continue
        for j in range(i):
            st = j
            en = j + i
            while en < n: 
                if (abs(a[st] - a[en])) == 0: st += i; en += i; continue
                f = gcd(f, abs(a[st] - a[en]))
                st = en
                en += i
        if f != 1: cnt += 1
    print(cnt)
            

    

C++ Code:

#include <bits/stdc++.h>
typedef long long ll;
#define int long long
#define PI acos(-1.0)
#define l1 cout << -1 << endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define endl "\n"
using namespace std;
using PII = pair<int,int>;
const int MAXN=2e5 + 9;
int t,a[MAXN];
int n;
queue<int> q;

void additional()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> t;
}

int check(int x)
{
	int temp = 0;
	for(int i = 1;i + x <= n;i ++)
	{
		temp = gcd(temp,a[i + x] - a[i]);
	}
	return temp != 1;
}

void solve()
{
	
	cin >> n;
	for(int i = 1;i <= n;i ++)
	{
		cin >> a[i];
	}
	int ans = 0;
	for(int i = 1;i * i <= n;i ++)
	{
		if(n % i == 0)
		{
			ans += check(i);
			if(i * i != n)
			{
				ans += check(n / i);
			}
		}
	}
	cout << ans << '\n';
}

signed main()
{
	t = 1;
	additional();
	while(t --)
	{
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence